﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Text;
using Business;
using System.Data;
using System.Web.Security;
public partial class admin_TechinicalDataSheet : @base {
    DataTable components = new DataTable();
    ReportServices rs = new ReportServices();
    DataSheetService ds = new DataSheetService();
    // mshtml.HTMLWindow2Class window;
    protected void Page_Load(object sender, EventArgs e)
    {


        if (UserDetails == null)
        {
             Response.Redirect("../Default.aspx");
        }
        if (!IsPostBack)
        {
            if (UserDetails.Role != "Admin")
            {
                TaskGridView.AutoGenerateDeleteButton = false;
                TaskGridView.AutoGenerateEditButton = false;
                Label1.Visible = false;
                Label2.Visible = false;
                txtAttribute.Visible = false;
                txtValue.Visible = false;
                Get_Button.Visible = false;
                save.Visible = false;
                CategoryManage.Visible = false;


            }

            MachineNameLabel.Text = "Technical Data Sheet for " + rs.getMachineNamebyID(MACHINEID);
            components = rs.getcomponentsByMachine(MACHINEID);
            foreach (DataRow component in components.Rows)
            {
                String componentName = component["componentName"].ToString();
                string componentID = component["component_id"].ToString();

                System.Web.UI.WebControls.ListItem ls = new System.Web.UI.WebControls.ListItem(componentName, componentID, true);
                DropDownClientList.Items.Add(ls);
            }
            Session["TaskTable"] = getTable();

            //Bind data to the GridView control.
            BindData();

        }
        else
        {
           Session["TaskTable"] = setSession();            
            
        }
    }
    protected DataTable setSession()
    {
        int tableNumber = TaskGridView.Rows.Count;
        DataTable taskTable = new DataTable("TaskList");

        // Create the columns.
        taskTable.Columns.Add("Attribute", typeof(string));
        taskTable.Columns.Add("Value", typeof(string));

        for (int i = 0; i < tableNumber; i++)
        {
            GridViewRow row = TaskGridView.Rows[i];
            string att = ((TextBox)(row.Cells[1].FindControl("TextBox1"))).Text.ToString();
            string val = ((TextBox)(row.Cells[1].FindControl("TextBox2"))).Text.ToString();
            DataRow tableRow = taskTable.NewRow();
            tableRow["Attribute"] = att;
            tableRow["Value"] = val;
            taskTable.Rows.Add(tableRow);

        }
        return taskTable;

    }
    protected void Component_Changed(object sender, EventArgs e)
    {

        Session["TaskTable"] = getTable();
        BindData();
    }
    protected DataTable getTable()
    {
        // Create a new table.
        DataTable taskTable = new DataTable("TaskList");

        // Create the columns.
        taskTable.Columns.Add("Attribute", typeof(string));
        taskTable.Columns.Add("Value", typeof(string));
        string component_id = DropDownClientList.SelectedValue.ToString();
        taskTable = ds.getDataByComponentID(component_id);
        //Persist the table in the Session object.
        if (taskTable.Rows.Count == 0)
        {
            string type = ds.getComponentType(component_id);
            DataTable temple = new DataTable();
            temple = ds.getDataByTypeID(type);
            if (temple == null || temple.Rows.Count == 0)
            {
                return taskTable;
            }
            foreach (DataRow r in temple.Rows)
            {
                DataRow tableRow = taskTable.NewRow();
                tableRow["Attribute"] = r["attribute"].ToString();
                tableRow["Value"] = "";
                taskTable.Rows.Add(tableRow);
            }

        }
        return taskTable;
    }
    /*  protected void TaskGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
      {
          TaskGridView.PageIndex = e.NewPageIndex;
          //Bind data to the GridView control.
          BindData();
      }*/

    protected void TaskGridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //Set the edit index.
        TaskGridView.EditIndex = e.NewEditIndex;
        //Bind data to the GridView control.
        BindData();
    }

    protected void TaskGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        //Reset the edit index.
        TaskGridView.EditIndex = -1;
        //Bind data to the GridView control.
        BindData();
    }

    protected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //Retrieve the table from the session object.
        DataTable dt = (DataTable)Session["TaskTable"];

        //Update the values.
        GridViewRow row = TaskGridView.Rows[e.RowIndex];
        dt.Rows[row.DataItemIndex]["Attribute"] = ((TextBox)(row.FindControl("TextBox1"))).Text;
        dt.Rows[row.DataItemIndex]["Value"] = ((TextBox)(row.FindControl("TextBox2"))).Text;//((TextBox)(row.Cells[2].Controls[0])).Text;        

        /*
        dt.Rows[e.RowIndex]["Attribute"] = e.NewValues["Attribute"].ToString();
        dt.Rows[e.RowIndex]["Value"] = e.NewValues["Value"].ToString();*/
        Session["TaskTable"] = dt;
        //Reset the edit index.
        TaskGridView.EditIndex = -1;

        //Bind data to the GridView control.
        BindData();
    }
    protected void TaskGridView_RowDeleting(Object sender, GridViewDeleteEventArgs e)
    {
        //int TableNumber = TaskGridView.Rows.Count;

        DataTable dt = (DataTable)Session["TaskTable"];
        int k = dt.Rows.Count;
        GridViewRow row = TaskGridView.Rows[e.RowIndex];

        string att = ((TextBox)(row.Cells[1].FindControl("TextBox1"))).Text.ToString();

       
            // dt.Rows[row.DataItemIndex].Delete();
            dt.Rows.RemoveAt(e.RowIndex);
            k = dt.Rows.Count;
            Session["TaskTable"] = dt;
            BindData();
       
    }
    protected void TaskGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int i = e.Row.DataItemIndex;
            e.Row.Cells[0].ForeColor = Color.Blue;
            TextBox tb1 = (TextBox)e.Row.FindControl("TextBox1");
            tb1.Text = ((DataTable)(Session["TaskTable"])).Rows[i]["Attribute"].ToString();

            // tb1.Text = showData.Rows[i]["Attributes"].ToString();
            TextBox tb2 = (TextBox)e.Row.FindControl("TextBox2");
            tb2.Text = ((DataTable)(Session["TaskTable"])).Rows[i]["Value"].ToString();
            
        }

    }
    private void BindData()
    {
        TaskGridView.DataSource = Session["TaskTable"];
        TaskGridView.DataBind();
    }



    protected void Button1_Click(object sender, EventArgs e)
    {


        DataTable dt = (DataTable)Session["TaskTable"];
        int k = dt.Rows.Count;
        string component_id = DropDownClientList.SelectedValue.ToString();
        ds.updateComponentTechnicalData(dt, component_id);
        string jscript = "<script language='javascript'>alert('Edit successfully!')</script>";
        //Page.RegisterClientScriptBlock("key2ClientBlock", jscript);
        this.ClientScript.RegisterClientScriptBlock(this.GetType(), "key2ClientBlock", jscript);

    }
    /*   public void setPage(mshtml.HTMLWindow2Class JSFile1,mshtml.HTMLWindow2Class JSFile2 )
       {
           window = JSFile;
       }*/
    protected void AddAttribute(object sender, EventArgs e)
    {
        string attribute = txtAttribute.Text.ToString();
        string value = txtValue.Text.ToString();

        if (attribute == "")
        {

            string jscript = "<script language='javascript'>alert('Should input name of attribute')</script>";
            // Page.RegisterClientScriptBlock("keyClientBlock",jscript);
            this.ClientScript.RegisterClientScriptBlock(this.GetType(), "keyClientBlock", jscript);
        }
        else
        {
            
                int ok = 1;
                DataTable dt = (DataTable)Session["TaskTable"];
                foreach (DataRow r in dt.Rows)
                {
                    string att = r["Attribute"].ToString();
                    if (attribute == att)
                    {
                        ok = 0;
                        break;
                    }
                }
                if (ok == 0)
                {
                    string jscript = "<script language='javascript'>alert('Cannot add an old attribute again!')</script>";
                    // Page.RegisterClientScriptBlock("keyClientBlock",jscript);
                    this.ClientScript.RegisterClientScriptBlock(this.GetType(), "keyClientBlock", jscript);

                }
                else
                {
                    DataRow newAttribute = dt.NewRow();
                    newAttribute["Attribute"] = attribute;
                    newAttribute["Value"] = value;

                    dt.Rows.Add(newAttribute);
                    Session["TaskGridView"] = dt;
                    txtAttribute.Text = "";
                    txtValue.Text = "";
                }
           
        }
        BindData();

    }
    protected void CategoryManage_Click(object sender, EventArgs e)
    {
       Server.Transfer("ManageCategory.aspx");
    }
    protected void viewTrend_Click(object sender, EventArgs e)
    {
        Server.Transfer("TrendReport.aspx");
    }
    protected void LoginStatus1_LoggingOut(object sender, LoginCancelEventArgs e)
    {


        Session.Contents.Remove("UserDetails");
        Session.Abandon();
        Session.Contents.RemoveAll();
        FormsAuthentication.SignOut();
        Response.Redirect("../Default.aspx");
    }

}
